﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CountDown : MonoBehaviour
{
    const int m_nCountDown=3;
    public GameObject m_o3;
    public GameObject m_o2;
    public GameObject m_o1;
    public GameObject m_oGO;
    GameObject m_oNow;
    GameObject m_oNext;
    void Awake()
    {       
    }
    void OnEnable()
    {
        GlobalHelper.Event.SetActive(false);
        StartCoroutine(WaitForBegin(m_nCountDown));
    }
    IEnumerator WaitForBegin(int time)
    {
        float second = 1f;
        m_oNow = m_oGO;
        while (time > -1)
        {
            if (3 == time)
            {
                m_oNext = m_o3;
            }
            else if (2 == time)
            {
                m_oNext = m_o2;
            }
            else if (1 == time)
            {
                m_oNext = m_o1;
            }
            else if (0 == time)
            {
                m_oNext = m_oGO;
            }
            m_oNext.SetActive(true);
            m_oNow.SetActive(false);
            m_oNow = m_oNext;
            Image image = m_oNow.GetComponent<Image>();
            image.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
            image.color = new Color(1f, 1f, 1f, 1f);
            while (second > 0)
            {
                second -= Time.deltaTime;
                image.transform.localScale += new Vector3(0.6f*Time.deltaTime, 0.6f * Time.deltaTime, 0);
                image.color -= new Color(0, 0, 0, Time.deltaTime);
                yield return new WaitForEndOfFrame();
            }
            second = 1f;
            time--;
        }
        Game1.isSong = true;
        gameObject.SetActive(false);
    }
    void OnDisable()
    {
        GlobalHelper.Event.SetActive(true);
    }
}
